package mahan.data.mongodb;

import mahan.data.model.Customer;
import mahan.data.model.Order;
import mahan.data.model.Product;
import mahan.data.model.ShoppingCart;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;

public class MahanDB {
	public enum COLLECTION_NAMES {
		CART, CUSTOMER, ORDER, PRODUCT
	};

	private String host;
	private int port;
	private String dbName;

	private Mongo mongo;

	private DB db;

	public MahanDB(String host, int port, String dbName) {
		this.host = host;
		this.port = port;
		this.dbName = dbName;

		try {
			mongo = new Mongo(this.host, this.port);
			db = mongo.getDB(this.dbName);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public DBCollection createCollection(String collectionName) {
		DBCollection dbcoll = db.getCollection(collectionName);
		if (dbcoll == null)
			dbcoll = db.createCollection(collectionName, new BasicDBObject());
		return dbcoll;
	}

	public DB get() {
		return db;
	}

	public ShoppingCart getCart(int id) {
		DBCollection coll = db.getCollection(COLLECTION_NAMES.CART.name());
		BasicDBObject query = new BasicDBObject();

		query.put("cid", id);

		DBCursor cur = coll.find(query);

		while (cur.hasNext()) {
			DBObject dbo = cur.next();
			return ShoppingCart.from(dbo);
		}
		return null;
	}

	public Customer getCustomer(int id) {
		DBCollection coll = db.getCollection(COLLECTION_NAMES.CUSTOMER.name());
		BasicDBObject query = new BasicDBObject();

		query.put("id", id);

		DBCursor cur = coll.find(query);

		while (cur.hasNext()) {
			DBObject dbo = cur.next();
			return Customer.from(dbo);
		}
		return null;
	}

	public Order getOrder(int id) {
		DBCollection coll = db.getCollection(COLLECTION_NAMES.ORDER.name());
		BasicDBObject query = new BasicDBObject();

		query.put("id", id);

		DBCursor cur = coll.find(query);

		while (cur.hasNext()) {
			DBObject dbo = cur.next();
			return Order.from(dbo);
		}
		return null;
	}

	public Product getProduct(int id) {
		DBCollection coll = db.getCollection(COLLECTION_NAMES.PRODUCT.name());
		BasicDBObject query = new BasicDBObject();

		query.put("pid", id);

		DBCursor cur = coll.find(query);

		while (cur.hasNext()) {
			DBObject dbo = cur.next();
			return Product.from(dbo);
		}
		return null;
	}

	public void persist(Customer cust) {
		BasicDBObject dbo = new BasicDBObject();
		dbo.putAll(cust.get());
		DBCollection coll = createCollection(COLLECTION_NAMES.CUSTOMER.name());
		coll.save(dbo);
	}

	public void persist(Order order) {
		BasicDBObject dbo = new BasicDBObject();
		dbo.putAll(order.get());
		DBCollection coll = createCollection(COLLECTION_NAMES.ORDER.name());
		coll.save(dbo);
	}

	public void persist(Product product) {
		BasicDBObject dbo = new BasicDBObject();
		dbo.putAll(product.get());
		DBCollection coll = createCollection(COLLECTION_NAMES.PRODUCT.name());
		coll.save(dbo);
	}

	public void persist(ShoppingCart cart) {
		BasicDBObject dbo = new BasicDBObject();
		dbo.putAll(cart.get());
		DBCollection coll = createCollection(COLLECTION_NAMES.CART.name());
		coll.save(dbo);
	}
}